package com.ksl.kinglockguard.widget.common;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ksl.kinglockguard.R;

public class LoadFailedView extends LinearLayout {

	private TextView tv_failed_msg;
	private ImageView iv_failed_image;
	
	private LinearLayout lay_container;

	private Context mContext;

	public LoadFailedView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.mContext = context;
		inflate(context, R.layout.lock_view_load_failed, this);
		init();
		setListener();
	}

	public LoadFailedView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		inflate(context, R.layout.lock_view_load_failed, this);
		init();
		setListener();
	}

	public LoadFailedView(Context context) {
		this(context, null);
	}

	private void init() {
		tv_failed_msg = (TextView) findViewById(R.id.tv_failed_msg);
		iv_failed_image = (ImageView) findViewById(R.id.iv_failed_image);
		lay_container = (LinearLayout) findViewById(R.id.lay_container);
	}

	private void setListener() {
		lay_container.setOnClickListener(clickListener);
	}

	/**
	 * 设置加载失败的提示Icon
	 * 
	 * @param msg
	 */
	public void setFailedImage(int resId) {
		if (resId > 0) {
			iv_failed_image.setImageResource(resId);
		}
	}

	/**
	 * 设置加载失败的提示Icon
	 * 
	 * @param msg
	 */
	public void setFailedImageBitmap(Bitmap bitmap) {
		if (bitmap != null) {
			iv_failed_image.setImageBitmap(bitmap);
		}
	}

	/**
	 * 设置加载失败的提示文字
	 * 
	 * @param msg
	 */
	public void setFailedMsg(CharSequence msg) {
		tv_failed_msg.setText(msg);
	}

	/**
	 * 设置加载失败的提示文字
	 * 
	 * @param msg
	 */
	public void setFailedMsg(int resId) {
		tv_failed_msg.setText(resId);
	}

	public void setOnClickListener(OnClickListener l) {
		this.clickListener = l;
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
			mContext.startActivity(intent);
		}
	};
}
